package cn.ffcs.uec.salary.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.salary.action.bean.UecSalaryPlanEditBean;
import cn.ffcs.uec.salary.constant.EnumUecSalary;
import cn.ffcs.uec.salary.manager.UecSalaryPlanManager;
import cn.ffcs.uec.salary.model.UecSalaryPlan;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecSalaryPlanEditComposer extends BasePortletComposer {
	private static final long serialVersionUID = -7900667176311023922L;

	private UecSalaryPlanEditBean bean = new UecSalaryPlanEditBean();

	private String opType;

	@Resource
	private UecSalaryPlanManager uecSalaryPlanManager;

	UecSalaryPlan uecSalaryPlan;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecSalaryPlanEditWin() throws Exception {
		bindBean();
	}

	/**
	 * 页面初始化
	 * 
	 * @throws Exception
	 */
	public void bindBean() throws Exception {
		opType = StrUtil.strnull(arg.get("opType"));
		if (EnumUecSalary.ADD.getValue().equals(opType)) {
			bean.getUecSalaryPlanEditWin().setTitle("薪资方案新增");
		} else if (EnumUecSalary.MOD.getValue().equals(opType)) {
			bean.getUecSalaryPlanEditWin().setTitle("薪资方案修改");
			initPageBean();
		} else {
			bean.getUecSalaryPlanEditWin().setTitle("薪资方案查看");
			bean.getBtnToolBar().setVisible(false);
			PubUtil.disableComponent(bean.getUecSalaryPlanDiv(), true);
			bean.getStartyear().setDisabled(true);
			bean.getCyear().setDisabled(true);
			initPageBean();
		}
	}

	public void initPageBean() throws Exception {
		uecSalaryPlan = (UecSalaryPlan) arg.get("uecSalaryPlan");
		PubUtil.fillBeanFromPo(uecSalaryPlan, bean);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date date = null;

		String startyearStr = uecSalaryPlan.getStartyear();

		if (!StrUtil.isNullOrEmpty(startyearStr)) {
			try {
				date = sdf.parse(startyearStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			bean.getStartyear().setValue(date);
		}

		String cyearStr = uecSalaryPlan.getCyear();

		if (!StrUtil.isNullOrEmpty(cyearStr)) {
			try {
				date = sdf.parse(cyearStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			bean.getCyear().setValue(date);
		}
	}

	public void initPoModel() {
		PubUtil.fillPoFromBean(bean, uecSalaryPlan);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		
		String startyearStr = sdf.format(bean.getStartyear().getValue());
		uecSalaryPlan.setStartyear(startyearStr);
		
		String cyearStr = sdf.format(bean.getCyear().getValue());
		uecSalaryPlan.setCyear(cyearStr);
	}

	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}

		if (EnumUecSalary.ADD.getValue().equals(opType)) {
			uecSalaryPlan = UecSalaryPlan.newInstance();
			initPoModel();

			uecSalaryPlan.addOnly();
			Events.postEvent(Events.ON_OK, bean.getUecSalaryPlanEditWin(),
					uecSalaryPlan);
		} else if (EnumUecSalary.MOD.getValue().equals(opType)) {
			initPoModel();

			uecSalaryPlan.updateOnly();

			Events.postEvent(Events.ON_OK, bean.getUecSalaryPlanEditWin(),
					uecSalaryPlan);
		}

		onCancel();
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecSalaryPlanEditWin().onClose();
	}

	private String checkUacStaffData() {
		// BaseInfo Check
		if (StrUtil.isNullOrEmpty(bean.getCode().getValue())) {
			return "请填写薪资类别编码";
		} else if (StrUtil.checkBlank(bean.getCode().getValue())) {
			return "薪资类别编码中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getName().getValue())) {
			return "请填写薪资类别名称";
		} else if (StrUtil.checkBlank(bean.getName().getValue())) {
			return "薪资类别名称中有空格";
		} else if (!StrUtil.checkBlank(bean.getCperiod().getValue())
				&& bean.getCperiod().getValue().length() > 2) {
			return "起始期间长度超过2位数上限";
		} else if (!StrUtil.checkBlank(bean.getCperiod().getValue())
				&& bean.getCperiod().getValue().length() > 2) {
			return "最新业务期间长度超过2位数上限";
		} else {
			return null;
		}
	}
}
